package com.hjm.mall.service.impl;

import com.hjm.mall.api.param.AddressParam;
import com.hjm.mall.dao.AddressMapper;
import com.hjm.mall.dao.MallUserMapper;
import com.hjm.mall.entity.Address;
import com.hjm.mall.entity.MallUser;
import com.hjm.mall.service.AddressService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    AddressMapper addressMapper;
    @Autowired
    MallUserMapper mallUserMapper;
    @Override
    public boolean inset(Long userId,AddressParam addressParam) {
        Address address = new Address();
        BeanUtils.copyProperties(addressParam, address);
        //TODO: Long转int
        address.setUserId(Integer.parseInt(userId.toString()));
        if (address.getDef()){
            // 清除默认地址
            addressMapper.clearDefaultByUserId(userId);
        }
        return addressMapper.insert(address)>0;
    }

    @Override
    public boolean delete(int id) {
        return addressMapper.delete(id)>0;
    }

    @Override
    public boolean update(Long userId, int id, AddressParam addressParam) {
        Address address = new Address();
        BeanUtils.copyProperties(addressParam, address);
        address.setId(id);
        if (address.getDef()){
            // 清除默认地址
            addressMapper.clearDefaultByUserId(userId);
        }
        return addressMapper.update(address) > 0;
    }

    @Override
    public List<Address> getAddressListByUserId(Long userId) {
        return addressMapper.getAddressListByUserId(userId);
    }

    @Override
    public Address getAddressListByUserIdDefault(Long userId) {
        return addressMapper.getAddressListByUserIdDefault(userId);
    }
}
